
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

/**
 * 
 * @author pfister
 * delicious client based on httpclient
 *
 */
public class DeliciousGet {

	public static void main(String[] args) throws Exception {
		DefaultHttpClient httpclient = new DefaultHttpClient();

		httpclient.getState().setCredentials(AuthScope.ANY,
				new UsernamePasswordCredentials("pfiste333", "s"));

		HttpGet httpget = new HttpGet("https://api.del.icio.us/v1/posts/recent");

		System.out.println("executing request" + httpget.getRequestLine());
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();

		System.out.println("----------------------------------------");
		System.out.println(response.getStatusLine());
		if (entity != null) {
			System.out.println("Response content length: "
					+ entity.getContentLength());
			System.out.println("Chunked?: " + entity.isChunked());
			
			InputStream responseBody = entity.getContent();
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(responseBody);
			entity.consumeContent();

			XPath xpath = XPathFactory.newInstance().newXPath();
			NodeList nls = (NodeList) xpath.evaluate("posts/post", doc,
					XPathConstants.NODESET);
			System.out.println();
			for (int i = 0; i < nls.getLength(); i++) {
				NamedNodeMap t = nls.item(i).getAttributes();
				//if (i==0)
			    // for( int j = 0; j<t.getLength();j++)
			    //	System.out.println(t.item(j).getNodeName());
				
				String description = t.getNamedItem("description")
				.getNodeValue();
				String extended = t.getNamedItem("extended")
				.getNodeValue();
				String hash = t.getNamedItem("hash")
				.getNodeValue();
				String href = t.getNamedItem("href")
				.getNodeValue();
				String tag = t.getNamedItem("tag")
				.getNodeValue();
				String time = t.getNamedItem("time")
				.getNodeValue();
						
				System.out.println("description:" + description);
				System.out.println("extended:" + extended);
				System.out.println("hash:" + hash);
				System.out.println("href:" + href);
				System.out.println("tag:" + tag);
				System.out.println("time:" + time);
				System.out.println();
			
			}
		}
		System.exit(0);
	}

	public static void main_(String[] args) throws Exception {
		DefaultHttpClient httpclient = new DefaultHttpClient();

		httpclient.getState().setCredentials(
				// new AuthScope("api.del.icio.us", 443),
				AuthScope.ANY,
				new UsernamePasswordCredentials("pfiste333", "s8"));

		HttpGet httpget = new HttpGet("https://api.del.icio.us/v1/tags/get");

		System.out.println("executing request" + httpget.getRequestLine());
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();

		System.out.println("----------------------------------------");
		System.out.println(response.getStatusLine());
		if (entity != null) {
			System.out.println("Response content length: "
					+ entity.getContentLength());
			System.out.println("Chunked?: " + entity.isChunked());

			BufferedReader br = new BufferedReader(new InputStreamReader(entity
					.getContent(), "utf-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		}
		if (entity != null) {
			entity.consumeContent();
		}
	}
}
